﻿using Microsoft.TeamFoundation.VersionControl.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TeamExplorer.Merge.Helper
{
    public class Utility
    {
        public static string WorkspaceToPartialWorkspaceSpec(Workspace workspace)
        {
            if (workspace.VersionControlServer.TeamProjectCollection.HasAuthenticated && workspace.OwnerNameMatches(workspace.VersionControlServer.AuthorizedUser))
                return workspace.Name;
            return workspace.DisplayName;
        }

        public static DateTimeGroup GetNormalizedDateTimeGroup(DateTime dateTimeToNormalize)
        {
            DateTime now = DateTime.Now;
            DateTime startOfWeek = DateTimeExtensions.GetStartOfWeek(new DateTime(now.Year, now.Month, now.Day));
            DateTime dateTime1 = CultureInfo.CurrentCulture.Calendar.AddWeeks(startOfWeek, -1);
            DateTime dateTime2 = CultureInfo.CurrentCulture.Calendar.AddWeeks(startOfWeek, -2);
            DateTime dateTime3 = CultureInfo.CurrentCulture.Calendar.AddWeeks(startOfWeek, -3);
            DateTime dateTime4 = CultureInfo.CurrentCulture.Calendar.AddWeeks(startOfWeek, -4);
            if (dateTimeToNormalize.Date == now.Date)
                return DateTimeGroup.Today;
            if (dateTimeToNormalize.Date == now.Date.AddDays(-1))
                return DateTimeGroup.Yesterday;
            if (dateTimeToNormalize >= startOfWeek)
                return DateTimeGroup.ThisWeek;
            if (dateTimeToNormalize >= dateTime1)
                return DateTimeGroup.LastWeek;
            if (dateTimeToNormalize >= dateTime2)
                return DateTimeGroup.TwoWeeksAgo;
            if (dateTimeToNormalize >= dateTime3)
                return DateTimeGroup.ThreeWeeksAgo;
            return dateTimeToNormalize >= dateTime4 ? DateTimeGroup.FourWeeksAgo : DateTimeGroup.Older;
        }

        public static string GetDescriptionFromEnumValue(Enum value)
        {
            DescriptionAttribute attribute = value.GetType()
                .GetField(value.ToString())
                .GetCustomAttributes(typeof(DescriptionAttribute), false)
                .SingleOrDefault() as DescriptionAttribute;
            return attribute == null ? value.ToString() : attribute.Description;
        }

        public static T GetValueFromDescription<T>(string description)
        {
            var type = typeof(T);
            if (!type.IsEnum) throw new InvalidOperationException();
            foreach (var field in type.GetFields())
            {
                var attribute = Attribute.GetCustomAttribute(field,
                    typeof(DescriptionAttribute)) as DescriptionAttribute;
                if (attribute != null)
                {
                    if (attribute.Description == description)
                        return (T)field.GetValue(null);
                }
                else
                {
                    if (field.Name == description)
                        return (T)field.GetValue(null);
                }
            }
            throw new ArgumentException("Not found.", "description");
            // or return default(T);
        }
    }
}